package com.six.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.six.core.entity.Checkgoods;
import com.six.core.entity.Goods;
import com.six.core.entity.Offshelf;
import com.six.core.entity.Shop;
import com.six.core.util.JwtUtil;
import com.six.core.util.WebUtil;
import com.six.core.vo.GoodsNumUpdateVo;
import com.six.core.vo.GoodsVo;
import com.six.core.vo.Result;
import com.six.goods.mapper.CheckgoodsMapper;
import com.six.goods.mapper.OffshelfMapper;
import com.six.goods.mapper.ShopMapper;
import com.six.goods.service.GoodsService;
import com.six.goods.mapper.GoodsMapper;
import com.sun.webkit.graphics.WCRenderQueue;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author wang
* @description 针对表【sto_goods】的数据库操作Service实现
* @createDate 2023-02-27 09:56:34
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService{
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private OffshelfMapper offshelfMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private CheckgoodsMapper checkgoodsMapper;

    /**
     * 商品列表--分页查询
     * @param currentPage
     * @param pageSize
     * @param goodsVo
     * @return
     */
    @Override
    public Result seleAllGoods(Integer currentPage, Integer pageSize, GoodsVo goodsVo) {
        PageHelper.startPage(currentPage,pageSize);
        List<Goods> goods = goodsMapper.selectAllGoods(goodsVo);
        PageInfo<Goods> pageInfo=new PageInfo<>(goods);
        if (pageInfo!=null){
            return new Result(2000 ,"查询成功",pageInfo);
        }else {
            return new Result(5000, "查询失败");
        }
    }
    /**
     * 商品列表--分页查询
     * @param currentPage
     * @param pageSize
     * @param goodsVo
     * @return
     */
    @Override
    public PageInfo<Goods> seleGoods(Integer currentPage, Integer pageSize, GoodsVo goodsVo) {
        PageHelper.startPage(currentPage,pageSize);
        List<Goods> goods = goodsMapper.selectAllGoods(goodsVo);
        PageInfo<Goods> pageInfo=new PageInfo<>(goods);
        return  pageInfo;
    }

    //修改商品库存
    @Override
    public Integer updateGoodsNum(List<GoodsNumUpdateVo> goodsList) {
        for (GoodsNumUpdateVo goodsVo : goodsList) {
            QueryWrapper<Goods> wrapper = new QueryWrapper<>();
            wrapper.eq("goodsid",goodsVo.getGoodsid());
            Goods goods = goodsMapper.selectOne(wrapper);
            goods.setGoodsnum(goods.getGoodsnum()-goodsVo.getGoodsnum());
            UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("goodsnum",goods.getGoodsnum())
                    .eq("goodsid",goodsVo.getGoodsid());
            int i = goodsMapper.update(new Goods(), updateWrapper);
        }
        return 0;
    }

    /**
     * 前端--所有商品展示___分页
     * @param currentPage
     * @param pageSize
     * @param goodsVo
     * @return
     */
    @Override
    public Result selectGoodsByCatrgotyId(Integer currentPage, Integer pageSize, GoodsVo goodsVo) {
        PageHelper.startPage(currentPage,pageSize);
        List<Goods> goods = goodsMapper.selectGoodsByCatrgotyId(goodsVo);
        PageInfo<Goods> pageInfo=new PageInfo<>(goods);
        if (pageInfo!=null){
            return new Result(2000 ,"查询成功",pageInfo);
        }else {
            return new Result(5000, "查询失败");
        }
    }

    @Override
    public Result selectGoodsByCatrgoty(Integer currentPage, Integer pageSize, GoodsVo goodsVo) {
        PageHelper.startPage(currentPage,pageSize);
        List<Goods> goods = goodsMapper.selectGoodsByCatrgoty(goodsVo);
        PageInfo<Goods> pageInfo=new PageInfo<>(goods);
        if (pageInfo!=null){
            return new Result(2000 ,"查询成功",pageInfo);
        }else {
            return new Result(5000, "查询失败");
        }
    }

    /**
     * 根据 id查询商品所有信息
     * @param id
     * @return
     */
    @Override
    public Goods goodsbyid(Integer id) {
        Goods goods = goodsMapper.goodsbyid(id);
        if (goods!=null){
            return goods;
        }else {
            return null;
        }
    }
    @Override
    public Result goodsbyids(Integer id) {
        Goods goods = goodsMapper.goodsbyid(id);
        if (goods!=null){
            return new Result(2000,"查询成功",goods);
        }else {
            return new Result(5000,"查询失败");
        }
    }

    //前端--商家登录--商品管理
    @Override
    public Result selectAllGoodsByshopid(Integer currentPage, Integer pageSize, GoodsVo goodsVo) {
        PageHelper.startPage(currentPage,pageSize);
        //获取登录人的 shopid
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer shopId = (Integer) map.get("shopId");

        goodsVo.setShopid(shopId);

        List<Goods> goods = goodsMapper.selectAllGoodsByshopid(goodsVo);
        PageInfo<Goods> pageInfo=new PageInfo<>(goods);
        if (pageInfo!=null){
            return new Result(2000 ,"查询成功",pageInfo);
        }else {
            return new Result(5000, "查询失败");
        }
    }
    //前端--商家登录--商品管理--添加
    @Override
    public Result insertGoodsOne(Goods goods) {
        //生成随机数作为goodsID
        String goodsid = RandomStringUtils.randomAlphanumeric(9);
        //根据登录人的id获取 他的店铺id
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer tbUserid = (Integer) map.get("tbUserid");
        Integer shopid = shopMapper.selectShopidByUserid(tbUserid);

        // 存部分前端没有的商品信息
        goods.setGoodsid(goodsid);
        goods.setState(2);
        goods.setIsDeleted(0);
        goods.setShopid(shopid);
        goods.setGmtCreate(new Date());
        //添加到Goods商品信息表里
        int i = goodsMapper.insert(goods);

        //添加到审核表
        Checkgoods checkgoods = new Checkgoods();
        checkgoods.setGoodsid(goods.getGoodsid());
        checkgoods.setShopid(goods.getShopid());
        checkgoods.setGmtCreate(goods.getGmtCreate());
        checkgoods.setState(0);
        checkgoods.setCkstate(0);
        int insert = checkgoodsMapper.insert(checkgoods);
        if (i>0 && insert>0){
            return new Result(2000,"添加成功");
        }
        return new Result(5000,"添加失败");
    }

    //前端--商家登录--商家上下架商品
    @Override
    public Result soldout(Goods goods) {
        int i = goodsMapper.updateById(goods);
        if (i>0 ){
            return new Result(2000,"下架成功");
        }
        return new Result(5000,"下架失败");
    }

    //前端--商家登录--商家删除商品
    @Override
    public Result deteleGoods(Integer id) {
        int i = goodsMapper.deleteById(id);
        if (i>0 ){
            return new Result(2000,"删除成功");
        }
        return new Result(5000,"删除失败");
    }

    //前端--商家登录--商家修改商品
    @Override
    public Result updateGoods(Goods goods) {
        int i = goodsMapper.updateById(goods);
        if (i>0 ){
            return new Result(2000,"修改成功");
        }
        return new Result(5000,"修改失败");
    }

    //根据商店id查询店铺商品--拥有的商品
    @Override
    public Result selectByshopid(Integer currentPage, Integer pageSize,Integer shopid) {
        PageHelper.startPage(currentPage,pageSize);
        List<Goods> goods = goodsMapper.selectByshopid(shopid);
        PageInfo<Goods> pageInfo = new PageInfo<>(goods);
        if (pageInfo != null){
            return new Result(2000,"查询成功",pageInfo);
        }
        return new Result(5000,"查询失败");
    }
    //根据商店id查询店铺商品--店铺照片、名称
    @Override
    public Result selectByshopid1(Integer shopid) {
        Goods goods = goodsMapper.selectByshopid1(shopid);
        if (goods != null){
            return new Result(2000,"查询成功",goods);
        }
        return new Result(5000,"查询失败");
    }


    /**
     * 商品列表--上下架---修改商品状态
     * @param goods
     * @return
     */
    @Override
    public Result updateState(Goods goods){
        //获取登录人的id
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer empId = (Integer) map.get("empId");

        //根据 id修改(下架)
        goodsMapper.updateById(goods);

        Offshelf offshelf = new Offshelf();
        offshelf.setEmpid(empId);
        offshelf.setGoodsid(goods.getGoodsid());
        offshelf.setShopid(goods.getShopid());
        offshelf.setGmtModified(new Date());
        offshelf.setOfstate(0);
        offshelf.setRemark(goods.getCheckRenark());
        int i = offshelfMapper.insert(offshelf);
        if (i>0){
            return new Result(2000,"修改成功");
        }else {
            return new Result(5000,"修改失败");
        }
    }

    /**
     * 商品审核--分页查询
     * @param currentPage
     * @param pageSize
     * @param goodsVo
     * @return
     */
    @Override
    public Result queryGood(Integer currentPage, Integer pageSize, GoodsVo goodsVo) {
        PageHelper.startPage(currentPage,pageSize);
        List<Goods> goods = goodsMapper.queryGood(goodsVo);
        PageInfo<Goods> pageInfo=new PageInfo<>(goods);
        if (pageInfo != null) {
            return new Result(2000, "查询成功", pageInfo);
        }else {
            return new Result(5000 ,"查询失败");
        }
    }

}




